<?php

namespace App\Http\Controllers\Idea;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 意见反馈控制器
 */
class IdeaController extends Controller
{

    public function feedbackView(Request $request)
    {
        //登录页面
        if($request->method()=="GET"){
            return view('idea.idea');
        }
    }

    public function feedback(Request $request)
    {
        //登录页面
        if($request->method()!="POST"){
            return response()->json([
                'code'=>0,
                'msg'=>'异常提交',
                'data'=>[],
            ]);
        }
        $param = $request->all();
        //定义验证规则
        $rules = [
            'content'       => 'required',
        ];
        //定义报错信息
        $messages = [
            'content.required'      => '反馈内容不可为空',
        ];
        //将$res里面传过来的数据进行判断
        $validator =  Validator::make($param,$rules,$messages);

        if ($validator->fails()){
            return response()->json([
                'code'=>0,
                'msg'=>'反馈内容不可为空',
                'data'=>[],
            ]);
        }

        //登录检查
        $user = session('user');
        if(!$user){
            return response()->json([
                'code'=>403,
                'msg'=>'请先登录!',
                'data'=>[],
            ]);
        }
        $ins = [
            'user_id'=>$user->user_id,
            'username'=>$user->username,
            'content'=>$param['content'],
            'date'=>date('Y-m-d'),
            'created_at'=>date('Y-m-d H:i:s'),
            'updated_at'=>date('Y-m-d H:i:s'),
        ];

        $result = DB::table('feedback')->insert($ins);
        if($result){
            return response()->json([
                'code'=>1,
                'msg'=>"反馈成功",
                'data'=>[],
            ]);
        }
        return response()->json([
            'code'=>0,
            'msg'=>"反馈失败，请稍后再试！",
            'data'=>[],
        ]);




    }



}
